package com.sojpt.boot.controller.business;


import java.util.*;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyTableStorageAddValidator;
import com.sojpt.boot.controller.business.validator.LkyTableStorageUpdateValidator;
import com.sojpt.boot.ui.iview.IViewController;

import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyClass;
import com.sojpt.model.LkyTableStorage;
import com.sojpt.model.LkyTeacherReleaseRecord;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/fileLookAndDown/")
public class LkyTableStorageAdminController extends IViewController {


    @GetMapping("list")
    public void list() {
        String uid=getPara("uid");
        Map<String,String> map =new HashMap<>();
        map.put("cid",getPara("cid"));
        map.put("mid",getPara("mid"));
        map.put("coid",getPara("coid"));
        map.put("uid",getPara("uid"));
        System.out.println("map***************"+map.entrySet());
        //查找用户对应角色id
        String roId=Db.queryStr("select ROLE_ID from dic_user_role where USER_ID =?",uid);
        SqlPara sqlPara=null;
//        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(roId.equals("b40d665874274e399985083485a42785")){//总管理
            sqlPara = Db.getSqlPara("lky_table_storage.allStudentFileByGeneralManagement", map);
        }
        if(roId.equals("a6a1564fdea64acc975266566dc61607")){//学院权限
            sqlPara = Db.getSqlPara("lky_table_storage.allStudentFileByCollage", map);
        }
        if(roId.equals("7371f2ced4154b8aaab16cccf378e7c3")){//老师权限
            sqlPara=Db.getSqlPara("lky_table_storage.allStudentFileByTeacher",map);
        }
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    /**
     * 学生所填的表
     */
    @GetMapping("queryStudentFillTableRecord")
    public void queryStudentFillTableRecord() {
        String uid = getPara("uid");
        SqlPara sqlPara = Db.getSqlPara("lky_table_storage.queryStudentFillTableRecord", Kv.by("uid", uid));
        List<Record> records = Db.find(sqlPara);
        renderJson(ResultUtil.success(records));
    }


    /**
     * 学生填表记录
     */
    @GetMapping("queryFillTableRecord")
    public void queryFillTableRecord() {
        String uid = getPara("uid");
        int pageNumber = Integer.valueOf(getPara("pageNumber"));
        int pageSize = Integer.valueOf(getPara("pageSize"));
        SqlPara sqlPara = Db.getSqlPara("lky_table_storage.queryFillTableRecord", Kv.by("uid", uid));
        Page<Record> page = Db.paginate(pageNumber, pageSize, sqlPara);
        renderJson(page);
    }

    /**
     * 上传老师发布表——待定
     */
    @GetMapping("saveTeacherTable")
    public void saveTeacherTable() {
        LkyTeacherReleaseRecord lkyTeacherReleaseRecord=new LkyTeacherReleaseRecord();
        lkyTeacherReleaseRecord.setId(UUID.randomUUID().toString().replace("-", ""));
        //测试数据：tid=32c0d596ee6d477cbee74b640c34972b
        lkyTeacherReleaseRecord.setCid(getPara("cid"));
        lkyTeacherReleaseRecord.setTid(getPara("tid"));
        if(lkyTeacherReleaseRecord.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }


    @PostMapping("save")
    @Before(LkyTableStorageAddValidator.class)
    public void save() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(lkyTableStorage.save()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("update")
    @Before(LkyTableStorageUpdateValidator.class)
    public void update() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(lkyTableStorage.update()) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    /**
     * 上传成绩表
     */
    @PostMapping("saveAssessFile")
    public void saveAssessFile(){
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/fileLookAndDown/saveAssessFile");
        LkyTableStorage lkyTableStorage=getModel(LkyTableStorage.class,"lkyTableStorage");
        if(lkyTableStorage.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("上传成绩表成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("上传成绩表失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        LkyTableStorage lkyTableStorage = getModel(LkyTableStorage.class, "lkyTableStorage");
        if(LkyTableStorage.dao.deleteById(lkyTableStorage.getId())) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_table_storage.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
        } else {
            renderJson(Ret.fail());
        }
    }

}